package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.megaprosoft.modyimcarcare.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Search extends Activity {
	ResultSet rs;
	//String ServiceID;
	EditText regis ;
	TextView regisID,time,sum,date,textUser;
	boolean check = false;
	
	String RegisterIDT,timeHH,timeMM,sumT,dateT,ServiceIDT,
    ServiceTypeT,BrandT,GenerationT,ColorT,StartTimeT,EndTimeT,StatusT;
	Button sea;
	String idDetail,idGetCar;
	String NameInput,company;
	connect con = new connect(); 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		
		
		ImageView bth = (ImageView) findViewById(R.id.BTH);
		Button btn = (Button) findViewById(R.id.btPrice);
        sea = (Button) findViewById(R.id.btSearch);
		
		regisID = (TextView)findViewById(R.id.textView2);
		time = (TextView)findViewById(R.id.textView3);
		sum = (TextView)findViewById(R.id.textView4);
		date = (TextView)findViewById(R.id.textView5);
		textUser = (TextView) findViewById(R.id.textUser);
		
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");
		
		sea.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
		regis = (EditText) findViewById(R.id.edt);
/*		String sql1 = "SELECT id AS maxid FROM getCar WHERE id = '"+regis.getText().toString()+"' ";
		
		rs = con.selectData(sql1);
		try {
			rs.next();
			ServiceID = rs.getString("maxid");
			check = false;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			check = true;
		}
		
	*/	
		if(check){
			Toast.makeText(Search.this, "����շ���¹ö "+regis.getText().toString()+"����㹺�ԡ��", Toast.LENGTH_LONG).show();
		}else{
		
		
		String sql = "SELECT convert(varchar(50), convert(money, DataService.Sum), 1) AS Sum, "+
		"CustomerCar.ServiceID AS ServiceID, "+
		"CustomerCar.Hour AS Hour, "+
		"CustomerCar.Minute AS Minute, "+
		"CustomerCar.RegisterID AS RegisterID, "+
		"CustomerCar.Brand AS Brand, "+
		"CustomerCar.Generation AS Generation, "+
		"CustomerCar.Color AS Color, "+
		"CustomerCar.ServiceType AS ServiceType, "+
		"ServiceDetail_old.DTID AS DTID, "+
		"ServiceDetail_old.idGetCar AS idGetCar, "+
		"DataService.date AS date "+
		"FROM DataService " +

		"LEFT JOIN CustomerCar "+
		"on DataService.ServiceID = CustomerCar.ServiceID "+
		
        "LEFT JOIN ServiceDetail_old "+
        "on DataService.ServiceID = ServiceDetail_old.ServiceID "+
        
         "LEFT JOIN getCar "+
         "on getCar.id = ServiceDetail_old.idGetCar "+

		"WHERE getCar.id = '"+regis.getText().toString()+"' ";  
		
		rs = con.selectData(sql);
		try {
			rs.next();
			RegisterIDT = rs.getString("RegisterID");
			timeHH = rs.getString("Hour");
			timeMM = rs.getString("Minute");
			dateT = rs.getString("date");
			sumT = rs.getString("Sum");
			ServiceIDT = rs.getString("ServiceID");
			ServiceTypeT = rs.getString("ServiceType");
			BrandT = rs.getString("Brand");
			GenerationT = rs.getString("Generation");
			ColorT = rs.getString("Color");
			idDetail = rs.getString("DTID");
			idGetCar = rs.getString("idGetCar");
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//StartTimeT = rs.getString("Accesstime");
	
		regisID.setText(RegisterIDT);
		time.setText(timeHH+":"+timeMM);
		sum.setText(sumT);
		date.setText(dateT);
		textUser.setText(NameInput);
		
			} //Else
			}	
		});
		
		
	
		
		bth.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			 
				//Open Form Add
				Intent newActivityAdd = new Intent(Search.this,MainActivity.class);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				startActivity(newActivityAdd);

			}



			});
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(check){
					Toast.makeText(Search.this, "��سҤ��ҷ���¹ö", Toast.LENGTH_LONG).show();
				}else{

				
				Intent newActivityAdd = new Intent(Search.this,Receipt.class);
				newActivityAdd.putExtra("ID", idDetail);
				newActivityAdd.putExtra("idGetCar", idGetCar);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				startActivity(newActivityAdd);
				}
				
			}
		});
	}
	
	@Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("¡��ԡ��÷���¡��")
	        .setMessage("��Ѻ�˹����ѡ")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                        Intent newActivity = new Intent(getApplicationContext(),MainActivity.class);	                          
	                        newActivity.putExtra("name", NameInput);
	        				newActivity.putExtra("CompanyS", company);
	                        startActivity(newActivity);
	                        finish();
	                        overridePendingTransition(R.layout.fadeendout,R.layout.fadeendin);
	                    }
	                })
	                .setPositiveButton(android.R.string.no, null).create().show();
	    

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}

}
